package com.ruoyi.supervise.service.impl.decisupport;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.supervise.config.CityConfig;
import com.ruoyi.supervise.domain.decisupport.City;
import com.ruoyi.supervise.domain.decisupport.ConstructionProject;
import com.ruoyi.supervise.mapper.decisupport.ConstructionProjectMapper;
import com.ruoyi.supervise.service.decisupport.ConstructionProjectService;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;

@Service
@DataSource(value = DataSourceType.SLAVE)
public class ConstructionProjectServiceImpl implements ConstructionProjectService{

    @Autowired
    private ConstructionProjectMapper constructionProjectMapper;
    
    @Autowired
    private CityConfig cityConfig;

	@Override
	public List<ConstructionProject> selectAllPro(ConstructionProject cp) {
		// TODO Auto-generated method stub
		return constructionProjectMapper.selectAllPro(cp);
	}

	@Override
	public List<ConstructionProject> selectAllHC(ConstructionProject cp) {
		// TODO Auto-generated method stub
		return constructionProjectMapper.selectAllHC(cp);
	}

	@Override
	public List<ConstructionProject> selectAllME(ConstructionProject cp) {
		// TODO Auto-generated method stub
		return constructionProjectMapper.selectAllME(cp);
	}

	@Override
	public ConstructionProject selectAllProProvince(ConstructionProject cp) {
		// TODO Auto-generated method stub
		return constructionProjectMapper.selectAllProProvince(cp);
	}

	@Override
	public ConstructionProject selectAllHCProvince(ConstructionProject cp) {
		// TODO Auto-generated method stub
		return constructionProjectMapper.selectAllHCProvince(cp);
	}

	@Override
	public ConstructionProject selectAllMEProvince(ConstructionProject cp) {
		// TODO Auto-generated method stub
		return constructionProjectMapper.selectAllMEProvince(cp);
	}

	@Override
	public List<ConstructionProject> selectAllTable(ConstructionProject cp) {
		List<ConstructionProject> allPro=constructionProjectMapper.selectAllPro(cp);
		List<ConstructionProject> allHC=constructionProjectMapper.selectAllHC(cp);
		List<ConstructionProject> allME=constructionProjectMapper.selectAllME(cp);
		List<ConstructionProject> all=new ArrayList<ConstructionProject>();
		//把在yml中设置的城市,设置到ConstructionProject实体中
		for(City c:cityConfig.getCityList())
		{
			ConstructionProject conPro=new ConstructionProject();
			conPro.setProjectCityNum(c.getCityname());
			conPro.setTotalNum("0");
			conPro.setComparedTotalNum("0");
			conPro.setTotalMoney("0");
			conPro.setComparedTotalMoney("0");
			conPro.setTotalNumHC("0");
			conPro.setComparedTotalNumHC("0");
			conPro.setTotalMoneyHC("0");
			conPro.setComparedTotalMoneyHC("0");
			conPro.setTotalNumME("0");
			conPro.setComparedTotalNumME("0");
			conPro.setTotalMoneyME("0");
			conPro.setComparedTotalMoneyME("0");
			all.add(conPro);
		}
		for(ConstructionProject a:all)
		{
			if(allPro!=null) {
				for(ConstructionProject pro:allPro)
				{
					if(StrUtil.isNotBlank(pro.getProjectCityNum()) && pro.getProjectCityNum().equals(a.getProjectCityNum()))
					{
						a.setTotalNum(pro.getTotalNum());
						a.setComparedTotalNum(pro.getComparedTotalNum());
						a.setTotalMoney(pro.getTotalMoney());
						a.setComparedTotalMoney(pro.getComparedTotalMoney());
					}
				}
			}
		}
		for(ConstructionProject a:all)
		{
			if(allHC!=null) {
				for(ConstructionProject hc:allHC)
				{
					if(StrUtil.isNotBlank(hc.getProjectCityNumHC()) && hc.getProjectCityNumHC().equals(a.getProjectCityNum()))
					{
						a.setTotalNumHC(hc.getTotalNumHC());
						a.setComparedTotalNumHC(hc.getComparedTotalNumHC());
						a.setTotalMoneyHC(hc.getTotalMoneyHC());
						a.setComparedTotalMoneyHC(hc.getComparedTotalMoneyHC());
					}
				}
			}
		}
		for(ConstructionProject a:all)
		{
			if(allME!=null) {
				for(ConstructionProject me:allME)
				{
					if(StrUtil.isNotBlank(me.getProjectCityNumME()) && me.getProjectCityNumME().equals(a.getProjectCityNum()))
					{
						a.setTotalNumME(me.getTotalNumME());
						a.setComparedTotalNumME(me.getComparedTotalNumME());
						a.setTotalMoneyME(me.getTotalMoneyME());
						a.setComparedTotalMoneyME(me.getComparedTotalMoneyME());
					}
				}
			}
		}
		return all;
	}

	@Override
	public ConstructionProject selectAllProvinceTable(ConstructionProject cp) {
		ConstructionProject allProProvince=constructionProjectMapper.selectAllProProvince(cp);
		ConstructionProject allHCProvince=constructionProjectMapper.selectAllHCProvince(cp);
		ConstructionProject allME=constructionProjectMapper.selectAllMEProvince(cp);
		ConstructionProject allProvince=new ConstructionProject();
		allProvince.setProjectCityNum("全省");
		allProvince.setTotalNum(allProProvince.getTotalNum());
		allProvince.setComparedTotalNum(allProProvince.getComparedTotalNum());
		allProvince.setTotalMoney(allProProvince.getTotalMoney());
		allProvince.setComparedTotalMoney(allProProvince.getComparedTotalMoney());
		//allProvince.setTotalNumHC(allHCProvince.getTotalMoneyHC());
		allProvince.setTotalNumHC(allHCProvince.getTotalNumHC());
		allProvince.setComparedTotalNumHC(allHCProvince.getComparedTotalNumHC());
		allProvince.setTotalMoneyHC(allHCProvince.getTotalMoneyHC());
		allProvince.setComparedTotalMoneyHC(allHCProvince.getComparedTotalMoneyHC());
		allProvince.setTotalNumME(allME.getTotalNumME());
		allProvince.setComparedTotalNumME(allME.getComparedTotalNumME());
		allProvince.setTotalMoneyME(allME.getTotalMoneyME());
		allProvince.setComparedTotalMoneyME(allME.getComparedTotalMoneyME());
		return allProvince;
	}

	@Override
	public int countNum(ConstructionProject cp) {
		// TODO Auto-generated method stub
		return constructionProjectMapper.countNum(cp);
	}

	@Override
	public Double countPrice(ConstructionProject cp) {
		// TODO Auto-generated method stub
		return constructionProjectMapper.countPrice(cp);
	}

}
